package com.webdemo12.controller;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import com.webdemo12.pojo.User;
import com.webdemo12.pojo.exception.UserException;
import com.webdemo12.service.UserService;
@Controller
@RequestMapping("/user")
@SessionAttributes("loginUser")
public class UserController {
	@Resource
	UserService userService;
	//用户登录
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(String name,Model model){
		User user = userService.getUser(name);
		if(user==null){
			//异常应该是Service层抛出这里只是演示
			throw new UserException("no this user");
//			return "login";
		}
		//设置Session
		model.addAttribute("loginUser", user);
		return "redirect:/ssh/user/users";
	}
	//用户登录跳转
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login(){
		return "login";
	}
	//查询某个用户
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public String getUser(@PathVariable int id,Model model){
		User user = userService.getUser(id);
		model.addAttribute(user);
		return "user";
	}
	//显示所有用户
	@RequestMapping(value="/users",method=RequestMethod.GET)
	public String getUsers(Model model){
		List<User> users = userService.getUsers();
		model.addAttribute("users", users);
		return "show";
	}
	//添加用户
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String addUser(@Valid User user,BindingResult results,
			@RequestParam(required=false)MultipartFile files[]) throws IOException{
		if(results.hasErrors()){
			return "add";
		}
		String path = "D:\\Workstation\\archive\\open-frame-ssh\\ssh_annotation04\\src\\main\\webapp\\resources\\upload";
		for(MultipartFile file:files){
			if(file.isEmpty()){
				continue;
			}
			FileUtils.copyInputStreamToFile(file.getInputStream(), 
					new File(path+"/"+file.getOriginalFilename()));
		}
		userService.addUser(user);
		return InternalResourceViewResolver.REDIRECT_URL_PREFIX+"/ssh/user/users";
		//return "redirect:/user/users";//和上面的一个意思
	}
	//添加跳转
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String addUser(Model model){
		//这时候key=user(类型的首字母小写),当然也可以使用model.addAttribute("user", new User());
		//设置一个空的User对象是因为页面中使用了modelAttribute="user";
		model.addAttribute(new User());
		return "add";
	}
	//删除用户
	@RequestMapping(value="/{id}/delete",method=RequestMethod.GET)
	public String deleteUser(@PathVariable int id){
		User user = userService.getUser(id);
		userService.deleteUser(user);
		return "redirect:/ssh/user/users";
	}
	//更新用户
	@RequestMapping(value="/{id}/update",method=RequestMethod.POST)
	public String updateUser(@Valid User user,BindingResult results){
		if(results.hasErrors()){
			return "update";
		}
		userService.updateUser(user);
		return "redirect:/ssh/user/users";
	}
	//更新跳转
	@RequestMapping(value="/{id}/update",method=RequestMethod.GET)
	public String updateUser(@PathVariable int id,Model model){
		User user = userService.getUser(id);
		model.addAttribute("user", user);
		return "update";
	}
	//处理异常
	@ExceptionHandler(value={UserException.class})
	public String handlerException(Exception ex, HttpServletRequest request){
		request.setAttribute("ex", ex);
		return "404";
	}
}
